//
//  SYBaseViewController.m
//  weizhegou
//
//  Created by sujeking on 2020/3/24.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "SYBaseViewController.h"
#import "UIImage+Extension.h"
#import "ZCBaseViewModel.h"
#import <SDImageCache.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import <TZImageManager.h>
#import <UIScrollView+EmptyDataSet.h>


static CGFloat leftBtnWidth = 70;

@interface SYBaseViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, copy) void (^takePicCallBack)(NSMutableArray *result);

@property (nonatomic, copy) NSString *noDataTitle ;
@property (nonatomic, copy) NSString *noDataDes ;
@property (nonatomic, copy) NSString *noDataImageName ;
@property (nonatomic, strong) UIScrollView *myScrollView ;


@end

@implementation SYBaseViewController

- (void)setParams:(NSDictionary *)params {
  _params = params;
  if (params != nil) {
    self.navTitle = [params valueForKey:@"title"];
    self.bridge = [params valueForKey:@"bridge"];
  }
}

- (void)setNavTitle:(NSString *)navTitle {
  _navTitle = navTitle;
  self.navigationItem.title = navTitle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseSetupUI];
    self.isPush = YES;
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeUserInfo) name:@"removeUserInfo" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  self.view.frame = [UIScreen mainScreen].bounds;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

// MARK: - Init

- (void)baseSetNavBarStyle {
    self.navigationController.navigationBar.translucent = NO;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[self navbarShadowImage]];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorFromRGB(0xffffff),
                                                                    NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                                                                    };
    [self.navigationController.navigationBar setBackgroundImage:
     [UIImage imageFromColor:[generalToolBox getColorWithHexadecimalColorStr:@"0xF9150F"]] forBarMetrics:UIBarMetricsDefault];

}

- (void)baseSetupUI {
    [self baseSetNavBarStyle];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
    self.view.backgroundColor = UIColorFromRGB(0xf5f5f5);
    if (self.navigationController.childViewControllers.count > 1) {
        UIImage *im = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e618", 25, UIColorFromRGB(0xffffff))];
        im = [im imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.frame = CGRectMake(0, 0, leftBtnWidth, 44);
        [backBtn.titleLabel setAdjustsFontSizeToFitWidth:YES];
        [backBtn setTitleEdgeInsets:(UIEdgeInsets){0,10,0,0}];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [backBtn setImage:im forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(basePopAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        UIBarButtonItem *spac = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spac.width = 0;
        self.navigationItem.leftBarButtonItems = @[spac,back];
        self.baseBackItem = backBtn;
    }
}

// MARK: - Action

- (void)basePopAction {
    if (self.presentingViewController && !self.isPush)
    {
        [self dismissViewControllerAnimated:YES completion:^{
            // 返回回调
        }];
        return ;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setLeftItemWithTitle:(NSString *)title action:(SEL)sel {
    if (self == self.navigationController.childViewControllers.firstObject) {
        [self.baseLeftItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        [self.baseLeftItem setTitleColor:UIColorFromRGB(0x757575) forState:UIControlStateNormal];
    }
    self.baseLeftItem.tintColor = [UIColor redColor];
    [self.baseLeftItem setTitle:title forState:UIControlStateNormal];
    [self.baseLeftItem addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]  initWithCustomView:self.baseLeftItem];
    self.navigationItem.leftBarButtonItem = item;
}

- (void)setLeftItemWithIcon:(UIImage *)icon action:(SEL)sel {
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.baseLeftItem setImage:icon forState:UIControlStateNormal];
    [self.baseLeftItem setImage:[UIImage imageByApplyingAlpha:0.5 image:icon] forState:UIControlStateHighlighted];
    [self.baseLeftItem addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]  initWithCustomView:self.baseLeftItem];
    self.navigationItem.leftBarButtonItem = item;
}

- (void)setRightItemWithTitle:(NSString *)title action:(SEL)sel {
    if (self == self.navigationController.childViewControllers.firstObject) {
    } else {
        [self.baseRightItem01 setTitleColor:UIColorFromRGB(0x6888E1) forState:UIControlStateNormal];
    }
    [self.baseRightItem01 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.baseRightItem01 setTitle:title forState:UIControlStateNormal];
    [self.baseRightItem01 addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]  initWithCustomView:self.baseRightItem01];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)setRightItemWithIcon:(UIImage *)icon action:(SEL)sel {
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.baseRightItem01 setImage:icon forState:UIControlStateNormal];
    [self.baseRightItem01 setImage:[UIImage imageByApplyingAlpha:0.5 image:icon] forState:UIControlStateHighlighted];
    [self.baseRightItem01 addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]  initWithCustomView:self.baseRightItem01];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)setRightItemWithIcons:(NSArray <UIImage *>*)icons action:(NSArray<NSString *> *)sels {
    UIImage *icon1 = icons.firstObject;
    UIImage *icon2 = icons.lastObject;
    
    icon1 = [icon1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    icon2 = [icon2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self.baseRightItem01 setBackgroundImage:icon1 forState:UIControlStateNormal];
    [self.baseRightItem02 setBackgroundImage:icon2 forState:UIControlStateNormal];
    
    [self.baseRightItem01 setBackgroundImage:[UIImage imageByApplyingAlpha:0.5 image:icon1] forState:UIControlStateHighlighted];
    [self.baseRightItem02 setBackgroundImage:[UIImage imageByApplyingAlpha:0.5 image:icon2] forState:UIControlStateHighlighted];
    
    [self.baseRightItem01 addTarget:self action:NSSelectorFromString(sels.firstObject) forControlEvents:UIControlEventTouchUpInside];
    [self.baseRightItem02 addTarget:self action:NSSelectorFromString(sels.lastObject) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item01 = [[UIBarButtonItem alloc]  initWithCustomView:self.baseRightItem01];
    UIBarButtonItem *item02 = [[UIBarButtonItem alloc]  initWithCustomView:self.baseRightItem02];
    self.navigationItem.rightBarButtonItems = @[item01,item02];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)showAlbum:(void (^)(NSMutableArray *))callback {
    PHAuthorizationStatus author = [PHPhotoLibrary authorizationStatus];
    if (author == PHAuthorizationStatusRestricted || author == PHAuthorizationStatusDenied) {
        //无权限 引导去开启
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self.tzvc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        NSArray *fileNames = [assets valueForKeyPath:@"filename"];
        [weakSelf netupLoadFile2ServerWithFiles:photos names:fileNames callback:callback];
    }];
    
    [self.tzvc setDidFinishPickingVideoHandle:^(UIImage *coverImage, id asset) {
        NSString *fileName = [asset valueForKeyPath:@"filename"];
        __block NSArray *fileNames = @[fileName];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
        hud.label.text = @"处理中";
        
        [[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPreset640x480 success:^(NSString *outputPath) {
            NSData *data = [NSData dataWithContentsOfFile:outputPath];
            NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
            [weakSelf netupLoadVideo2ServerWithFiles:@[data] names:fileNames callback:callback];
        } failure:^(NSString *errorMessage, NSError *error) {
            NSLog(@"视频导出失败:%@,error:%@",errorMessage, error);
        }];

    }];
    
    [self presentViewController:self.tzvc animated:YES completion:nil];
}

- (void)showCamera:(void(^)(NSMutableArray *result))callback {
    _takePicCallBack = callback;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
    {
        // 无权限 引导去开启
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication]canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
        return;
    }
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        return;
    }
    
    UIImagePickerController *pickerCon = [[UIImagePickerController alloc]init];
    pickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
    pickerCon.allowsEditing = YES;//是否可编辑
    pickerCon.delegate = self;
    [self presentViewController:pickerCon animated:YES completion:nil];
}


- (void)setTableView:(UIScrollView*)scrollView
     noDataWithTitle:(NSString*)title
                 des:(NSString*)des
        andImageName:(NSString*)imageName
{
    self.noDataDes = des;
    self.noDataTitle = title;
    self.noDataImageName = imageName;
    self.myScrollView = scrollView;
    self.myScrollView.emptyDataSetSource = self;
    self.myScrollView.emptyDataSetDelegate = self;
}


#pragma mark ---- DZNEmptyDataSource delegate ----
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{//  空数据的title提示
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    NSString *text = self.noDataTitle ;
    UIFont *font = [UIFont systemFontOfSize:15.0];
    UIColor *textColor = [generalToolBox getColorWithHexadecimalColorStr:@"#999999"];
    
    if (!text) {
        text = @"暂无数据";
    }
    
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}


- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{// 空数据的内容提示
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSString *text = self.noDataDes ;
    UIFont *font = [UIFont boldSystemFontOfSize:15.0];
    UIColor *textColor = [generalToolBox getColorWithHexadecimalColorStr:@"#545454"];
    
    if (!text) {
        return nil;
    }
    
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    if (paragraph) [attributes setObject:paragraph forKey:NSParagraphStyleAttributeName];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
    return attributedString;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
// 空数据的image提示
    if (self.noDataImageName) {
        return [UIImage imageNamed:self.noDataImageName];
    } else {
        return [UIImage imageNamed:@"nodata_normal_1"];
    }
}

- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{// 空数据的动画提示
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

-(void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self didTapView];
}

-(void)didTapView{
    
}

// MARK: - UIPickerViewDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSString *fileName = [[self getNowTimeTimestamp2] stringByAppendingString:@".png"];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self netupLoadFile2ServerWithFiles:@[image] names:@[fileName] callback:self.takePicCallBack];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)setMaxleft:(BOOL)maxleft {
    _maxleft = maxleft;
    if (maxleft) {
        self.baseBackItem.frame = CGRectMake(0, 0, 200, 44);
    }
}

// MARK: - RCTBridgeDelegate

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}


// MARK: - NET

- (void)netupLoadFile2ServerWithFiles:(NSArray <UIImage *>*)files names:(NSArray <NSString *>*)fileNames callback:(void (^)(NSMutableArray *))callback {
    
    dispatch_group_t group = dispatch_group_create();
    NSMutableArray *results = [NSMutableArray new];
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    __weak typeof(self) weakSelf = self;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"上传中";
    
    for (NSInteger i = 0; i < files.count; i++) {
        dispatch_group_enter(group);
        [ZCBaseViewModel uploadFile2ServerWithImg:files[i] fileName:fileNames[i] owner:self complete:^(ZCErrorCode code, NSString * _Nonnull msg, id  _Nonnull info) {
            if (code == kHXSNoError) {
                [results addObject:info];
            } else if (code == kHXSInvalidTokenError) {
//                WZGLoginVC *vc = [[WZGLoginVC alloc] init];
//                [vc setGoodsVCCallBack:^(NSInteger type) {
//
//                }];
//                UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
//                nvc.modalPresentationStyle = UIModalPresentationFullScreen;
//                [weakSelf.navigationController presentViewController:nvc animated:YES completion:nil];
            } else {
                [weakSelf.view makeToast:msg duration:2.0 position:CSToastPositionCenter];
            }
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        });
        if (callback) {
            NSMutableArray *res = [NSMutableArray new];
            for (NSArray *arr in results) {
                [res addObject:arr.firstObject];
            }
            callback(res);
        }
    });
}

- (void)netupLoadVideo2ServerWithFiles:(NSArray *)files names:(NSArray <NSString *>*)fileNames callback:(void (^)(NSMutableArray *))callback {
    
    dispatch_group_t group = dispatch_group_create();
    NSMutableArray *results = [NSMutableArray new];
    
    NSString *fileName = fileNames.firstObject;
    
    NSArray *names = [fileName componentsSeparatedByString:@"."];
    
    fileName = [names.firstObject stringByAppendingPathExtension:@"mp4"];
    
    __weak typeof(self) weakSelf = self;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"上传中";
    
    for (NSInteger i = 0; i < files.count; i++) {
        dispatch_group_enter(group);
        [ZCBaseViewModel uploadFile2ServerWithVideo:files[i] fileName:fileName owner:self complete:^(ZCErrorCode code, NSString * _Nonnull msg, id  _Nonnull info) {
            if (code == kHXSNoError) {
                [results addObject:info];
            } else if (code == kHXSInvalidTokenError) {
//                WZGLoginVC *vc = [[WZGLoginVC alloc] init];
//                [vc setGoodsVCCallBack:^(NSInteger type) {
//
//                }];
//                UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
//                nvc.modalPresentationStyle = UIModalPresentationFullScreen;
//                [weakSelf.navigationController presentViewController:nvc animated:YES completion:nil];
            } else {
                [weakSelf.view makeToast:msg duration:2.0 position:CSToastPositionCenter];
            }
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        });
        if (callback) {
            NSMutableArray *res = [NSMutableArray new];
            for (NSArray *arr in results) {
                [res addObject:arr.firstObject];
            }
            callback(res);
        }
    });
}

// MARK: - GET

- (NSUserDefaults *)userDefault {
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    return ud;
}

- (TZImagePickerController *)tzvc {
    if (_tzvc == nil) {
        _tzvc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
        _tzvc.allowPreview = NO;
    }
    return _tzvc;
}

- (ZCBaseUserModel *)userModel {
    ZCBaseUserManager *manager = [ZCBaseUserManager shareInstance];
    ZCBaseUserModel *userModel = manager.userModel;
    return userModel;
}

- (UIButton *)baseLeftItem {
    if (_baseLeftItem == nil) {
        _baseLeftItem = [UIButton buttonWithType:UIButtonTypeCustom];
        _baseLeftItem.frame = CGRectMake(0, 0, 44, 44);
    }
    return _baseLeftItem;
}

- (UIButton *)baseRightItem01 {
    if (_baseRightItem01 == nil) {
        _baseRightItem01 = [UIButton buttonWithType:UIButtonTypeCustom];
        _baseRightItem01.titleLabel.font = [UIFont systemFontOfSize:15];
        _baseRightItem01.titleLabel.textColor = UIColorFromRGB(0xf8f8fc);
        _baseRightItem01.frame = CGRectMake(0, 0, 44, 44);
    }
    return _baseRightItem01;
}

- (UIButton *)baseRightItem02 {
    if (_baseRightItem02 == nil) {
        _baseRightItem02 = [UIButton buttonWithType:UIButtonTypeCustom];
        _baseRightItem02.frame = CGRectMake(0, 0, 44, 44);
    }
    return _baseRightItem02;
}

- (UIImage *)navbarShadowImage {
    return [UIImage imageNamed:@"icon_navbar_line"];
}

- (UIView *)tableViewFooterView {
    UIView *fv = [UIView new];
    fv.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120);
    UINib *nib = [UINib nibWithNibName:@"SKNormalFooterView" bundle:nil];
    UIView *view = [nib instantiateWithOwner:self options:nil].firstObject;
    view.frame = fv.bounds;
    [fv addSubview:view];
    return fv;
}

- (void)showAlert:(NSString *)title
          content:(NSString *)content
       actionStr1:(NSString *)as1
            style:(UIAlertActionStyle)style
        actionAc1:(void(^)(UIAlertAction * _Nonnull action))block1
       actionStr2:(NSString *)as2
            style:(UIAlertActionStyle)style2
         actionAc:(void(^)(UIAlertAction * _Nonnull action))block2 {
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:as1 style:style handler:block1];
    [ac addAction:action1];
    if (as2 != nil) {
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:as2 style:style2 handler:block2];
        [ac addAction:action2];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self presentViewController:ac animated:YES completion:nil];
    });
}

- (NSString *)getNowTimeTimestamp2{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    return timeString;
}


- (void)hiddenNodataView {
    UIView *view = [self.view viewWithTag:9999];
    if (view != nil) {
        [view removeFromSuperview];
    }
}

- (NSString *)timestanp {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSInteger timeint = ceil([[NSDate date] timeIntervalSince1970] * 1000);
    NSString *timer_z = @(timeint).stringValue;
    return timer_z;
}

- (void)dealloc{
    NSLog(@"========line:%d,Func:%s,Class:%@",__LINE__,__func__,self);
}

@end


@implementation BaseViewModel

RCT_EXPORT_MODULE()

- (NSArray<NSString *> *)supportedEvents {
  return @[@"vmbase"];
}

- (void)sendMsg2RNWithCmd:(BaseViewModelCmd)cmd params:(NSDictionary *)dict {
  NSMutableDictionary *mdict = @{@"cmd":@(cmd)}.mutableCopy;
  [mdict addEntriesFromDictionary:dict];
  [self sendEventWithName:@"vmbase" body:mdict];
}

+ (void)sendMsg2RNWithCmd:(BaseViewModelCmd)cmd bridge:(RCTBridge *)bridge params:(NSDictionary *)dict {
  BaseViewModel *vm = [[BaseViewModel alloc] init];
  vm.bridge = bridge;
  [vm sendMsg2RNWithCmd:cmd params:dict];
}


@end

